package com.spring.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.blog.common.constant.Constants;
import com.spring.blog.common.util.BeanTransUtils;
import com.spring.blog.exception.BlogException;
import com.spring.blog.mapper.BlogInfoMapper;
import com.spring.blog.mapper.UserInfoMapper;
import com.spring.blog.pojo.dataobject.BlogInfo;
import com.spring.blog.pojo.dataobject.UserInfo;
import com.spring.blog.pojo.request.AddRequest;
import com.spring.blog.pojo.request.UpdateRequest;
import com.spring.blog.pojo.response.BlogInfoResponse;
import com.spring.blog.pojo.response.GetUserInfoResponse;
import com.spring.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.NORMAL);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);
        //处理返回给前端的数据，剔除一些多余的属性
        List<BlogInfoResponse> collect = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean add(AddRequest addRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addRequest,blogInfo);
       try{
           int insert = blogInfoMapper.insert(blogInfo);
           if(insert==1){
               log.info("发布博客成功");
               return true;
           }
           return false;
       }catch (Exception e){
           throw new BlogException("发布博客失败");
       }
    }

    @Override
    public boolean delete(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.DELETE);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            return i==1;
        }catch (Exception e){
            throw new BlogException("删除博客失败");
        }
    }

    @Override
    public boolean update(UpdateRequest updateRequest) {
        //记得处理异常
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(updateRequest,blogInfo);
        try{
            int i = blogInfoMapper.updateById(blogInfo);
            if(i==1){
                return true;
            }
            return false;

        }catch (Exception e){
            throw new BlogException("更新博客失败");
        }
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getId,blogId);
        BlogInfo blogInfo = blogInfoMapper.selectOne(queryWrapper);
        return BeanTransUtils.trans(blogInfo);
    }
}
